/*
 * @Author: 王炎 yan.wang@qiyuesuo.com
 * @Date: 2022-10-11 12:17:09
 * @LastEditors: 王炎 yan.wang@qiyuesuo.com
 * @LastEditTime: 2024-09-30 16:51:15
 * @FilePath: /http-proxy/index.js
 * @Description: 代理接入，通过 http 代理将所有请求转发到 http://localhost:4396 端口。
 */

const http = require('http')
const Koa = require('koa')
const tunnelProxy = require('./packages/tunnel')
const middlewares = require('./middleware')
const { httpsServer } = require('./packages/core-server')

const app = new Koa()
const server = http.createServer()

middlewares.forEach(mv => {
    app.use(mv)
})

// 处理 https 请求
tunnelProxy(server)

// 处理 http 请求，将 http 请求传给 app 处理
// server.on('request', app.callback())

server.on('request', (req, res) => {
    httpsServer.emit('request', req, res)
})

server.listen(4396, () => {
    console.log('代理接入入口： http://localhost:4396')
})

globalThis.process.on('uncaughtException', (err) => {
    console.log(err)    
});
